<?php
/**
 * Created by PhpStorm.
 * User: m1514
 * Date: 2016/12/29
 * Time: 8:38
 */

namespace Admin\Controller;


use Common\Model\BrandLogoModel;

class BrandLogoController extends LoginController
{
    public function index()
    {
        $indexData=(new BrandLogoModel())->select();
//        dd($indexData);die;
        $this->assign('indexData',$indexData);
        //显示模板
        $this->display();
    }
    //添加
    public function add(){
        if(IS_POST){//是否为为POST提交
            $data=I('post.');
            $data['logo']=$data['detailPic']['0'];
//            dd($data);die;
            $this->store(new BrandLogoModel(),$data,function(){
                $this->success('添加成功',u('index'));die;
            });
        }
        //显示模板
        $this->display();
    }
    //异步上传图片
    public function ajaxUpload(){
        //调用框架上传类来处理
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        //current  获取兼键值，在这转为一维数组
        $info   =   current($upload->upload());
        $path =  'Uploads/' . $info['savepath'] . $info['savename'];
        echo $path;exit;
    }
    //异步删除图片
    public function ajaxDelImg()
    {
        if(IS_AJAX){
            $path = $_POST['path'];
           // var_dump($path);die;
            if(file_exists($path)){
                unlink($path);
            }
        }
    }
    //修改
    public function edit(){
        $bid=I('get.bid');
        $editData=(new BrandLogoModel())->find($bid);
        $this->assign('editData',$editData);
        if(IS_POST){
            $data=I('post.');
            $data['bid']=I('get.bid');
            $data['logo']=$data['detailPic']['0'];
//            dd($data);die;
            $this->store(new BrandLogoModel(),$data,function(){
               $this->success('修改成功',u('index'));die;
            });
        }
        //显示模板
        $this->display();
    }
    //删除
    public function del(){
        $bid=I('get.bid');
        $data=(new BrandLogoModel())->delete($bid);
//        dd($data);die;
        if($data){
            $this->success('删除成功',u('index'));die;
        }
    }
}